# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=iproute2
pkgver=5.7.0
pkgrel=1
pkgdesc="IP Routing Utilities"
url="https://www.linuxfoundation.org/collaborate/workgroups/networking/iproute2"
arch="all"
license="GPL-2.0-or-later"
install="$pkgname.post-install"
makedepends="bison flex bash iptables-dev elfutils-dev"
subpackages="$pkgname-doc $pkgname-bash-completion:bashcomp:noarch $pkgname-minimal $pkgname-tc $pkgname-ss"
source="https://kernel.org/pub/linux/utils/net/iproute2/iproute2-$pkgver.tar.xz
	fix-install-errors.patch
	musl-fixes.patch"

# secfixes:
#   5.1.0-r0:
#     - CVE-2019-20795

prepare() {
	default_prepare
	sed -i '/^TARGETS=/s: arpd : :' misc/Makefile
	sed -i 's:/usr/local:/usr:' tc/m_ipt.c include/iptables.h
	sed -i -e 's:=/share:=/usr/share:' \
		-e 's:-Werror::' Makefile
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var
	make CCOPTS="-D_GNU_SOURCE $CFLAGS" V=1
}

package() {
	make -j1 DESTDIR="$pkgdir" install
}

minimal() {
	replaces="$pkgname"
	pkgdesc="IP Routing Utilities (/sbin/ip only)"
	install_if="$pkgname=$pkgver-r$pkgrel"

	install -d -m755 "$subpkgdir"/sbin

	mv "$pkgdir"/sbin/ip "$subpkgdir"/sbin/
	mv "$pkgdir"/etc "$subpkgdir"/etc
}

tc() {
	replaces="$pkgname"
	pkgdesc="IP Routing Utilities (traffic control and XDP support)"
	install_if="$pkgname=$pkgver-r$pkgrel"

	install -d -m755 "$subpkgdir"/sbin

	mv "$pkgdir"/sbin/tc "$subpkgdir"/sbin/
	mv "$pkgdir"/usr "$subpkgdir"/usr
}

ss() {
	replaces="$pkgname"
	pkgdesc="IP Routing Utilities (socket statistics tool)"
	install_if="$pkgname=$pkgver-r$pkgrel"

	install -d -m755 "$subpkgdir"/sbin

	mv "$pkgdir"/sbin/ss "$subpkgdir"/sbin/
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	mkdir -p "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/share/bash-completion \
		"$subpkgdir"/usr/share

	rmdir -p "$pkgdir"/usr/share 2>/dev/null || true
}

sha512sums="d5ef68e0eb8e84fbff68c2e7dd4097b9ceb438c928d17ede4130876802dfeafe8bf08d82b4fd39005314e0679c0b261aaa3d6a0a2ff364074a43a043a855a634  iproute2-5.7.0.tar.xz
24fc2a901650e11f80bcaa82c839e70c21aafdf3c5b8a357d932d066a0b98ae2ec8379fc17a0a16a1b5b4fa5edc131179c10fc02e55d6101701df5a09966912c  fix-install-errors.patch
2e3558caddf814da8c4d78c74eddb7a659d6f94b93de5396bdd995e2333e3cd656f9c936ac7a5a86d0477abc27a92550582575ab4ed19fc2ec0d9b6699cd612c  musl-fixes.patch"
